 <!-- 每日推荐歌单区域(登录模式) -->
<template>
  <div v-if="personalized.length > 0">
    <div class="titleHead">
      <div style="color: #ec4141">每日推荐歌单</div>
      <div class="more" @click="more()">换一批</div>
    </div>
    <hr />
    <el-row :gutter="12" style="margin-bottom: 40px">
      <el-col :md="6" :sm="12" :xs="24" v-for="item in personalizedData" :key="item.id" style="margin-bottom: 10px">
        <el-card shadow="hover" :body-style="{ padding: '0px' }">
          <div class="count">
            <i class="el-icon-video-play"></i>{{
                item.playcount >= 10000
                  ? (item.playcount / 10000).toFixed(0) + "万"
                  : item.playcount
            }}
          </div>
          <el-image :src="item.picUrl" fit="cover" @click="getItem(item.id)" lazy>
            <div slot="placeholder" class="image-slot">
              <el-image lazy src="https://s4.music.126.net/style/web2/img/default/default_album.jpg"></el-image>
            </div>
          </el-image>
        </el-card>
        <div class="title">{{ item.name }}</div>
      </el-col>
    </el-row>
    <div class="loading" v-if="loading === true">
      <i class="el-icon-loading"></i>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from 'vuex';

export default {
  computed: {
    ...mapState(['cookie', 'ifPlay']),
  },
  mounted() {
    if (localStorage.getItem('cookie') !== null) {
      this.getCookie = localStorage.getItem('cookie');
      this.getSongsList(this.getCookie);
    }
  },
  beforeRouteLeave(to, from, next) {
    this.loading = true
    next()
  },
  watch: {
    cookie(newVal, oldVal) {
      this.getCookie = '';
      this.getCookie = newVal.cookie;
      this.getSongsList(this.getCookie);
    },
  },
  data() {
    return {
      getCookie: '',
      personalized: [],
      number: 4,
      personalizedData: [],
      count: 0,
      loading: false,
    };
  },
  methods: {
    ...mapActions(['saveSongList', 'clickPlay']), // 调用VUEX中的保存歌单信息的函数
    // 获取每日推荐歌单需要传入cookie
    async getSongsList(e) {
      this.personalized = [];
      this.$api.recResource({ cookie: e }).then((ret) => {
        ret.recommend.map((item, i) => {
          this.personalized.push({
            id: item.id,
            picUrl: item.picUrl,
            name: item.name,
            copywriter: item.copywriter,
            playcount: item.playcount,
          });
        });
        this.count = this.personalized.length;
        this.personalizedData = this.personalized.slice(0, this.number);
      });
    },

    // 查看更多功能
    more() {
      this.number += 4;
      this.personalizedData = [];
      this.personalizedData = this.personalized.slice(
        this.number,
        this.number + 4,
      );
      if (this.number > this.count) {
        this.personalizedData = [];
        this.number = 4;
        this.personalizedData = this.personalized.slice(0, this.number);
      }
    },

    // 跳转到歌单明细
    async getItem(e) {
      this.loading = true;
      this.$api.getPlayList({ id: e, timestamp: this.$utils.timestamp(), cookie: localStorage.getItem('cookie') }).then((ret) => {
        if (ret.code === 200) {
          this.saveSongList({ songList: ret.playlist }); // 获取到的明细存入VUEX
          this.$router.push(`/songList?id=${e}`);
          this.clickPlay({ ifPlay: true });
        } else {
          this.$message.error('超时获取失败!!!');
        }
      });
    },
  },
};
</script>

<style scoped>
.titleHead {
  display: flex;
  justify-content: space-between;
  font-size: 25px;
}

.more {
  color: #ec4141;
  text-align: center;
  cursor: pointer;
}

.el-col {
  overflow: hidden;
}

.el-card {
  position: relative;
}

.title {
  font-weight: 700;
  color: #ec4141;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.el-image {
  width: 100%;
  height: 100%;
}

.count {
  position: absolute;
  z-index: 10;
  color: #fff;
  background-image: linear-gradient(#080808, #aca5a538);
  width: 100%;
  height: 20px;
  text-align: right;
}

.loading {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
}

.el-icon-loading {
  position: fixed;
  font-size: 50px;
  left: 50%;
  top: 50%;
  color: #ec4141;
}
</style>
